﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Linq.Expressions;
using System.Reflection.Emit;
using Hyper.ComponentModel;

namespace SimpleMap.Setters
{
    
    /// <summary>
    /// Maps a value in the target object using properties.
    /// </summary>
    public class ReflectionPropertyValueSetter<T> : IValueSetter<T>
    {

        private PropertyInfo _property;

        public string Name
        {
            get { return _property.Name; }
        }

        public ReflectionPropertyValueSetter(PropertyInfo property)
        {
            if (property == null) { throw new ArgumentNullException("property"); }
            
            _property = property;
        }

        /// <summary>
        /// Sets the value in the target object
        /// </summary>
        /// <param name="target">Object to update</param>
        /// <param name="fieldValue">The value to set</param>
        public void SetValue(T target, object fieldValue)
        {
            if (fieldValue == DBNull.Value)
            {
                _property.SetValue(target, null, null);
            }
            else
            {
                _property.SetValue(target, fieldValue, null);
            }

        }

        public override string ToString()
        {
            return this._property.Name;
        }

    }


    internal delegate void LateBoundPropertySet(object target, object value);

   
  
}

